package com.secneo.appshare.activities;


import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.secneo.appshare.AppShareApplication;
import com.secneo.appshare.R;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.common.AppSharePreference;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.common.Util;

public class AppshareAlarmActivity extends Activity{
	public CheckBox pushCheck=null;
	boolean isNumber=false;
	String appIconString="";
	String appTitle="";
	String pushTitle="";
	String pushMess="";
	String packageName="";
	String pushYOBOUrl="";
	String pushFirstButton="";
	String pushSecondButton="";
	int pushStatus=1;
	Drawable appIcon=null;
	String pushUrl=Constants.URL+Constants.PUSH_MESSAGE;
	public Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				showDialog();
				break;
			case 2:
				AppshareAlarmActivity.this.finish();
				break;
			default:
				AppshareAlarmActivity.this.finish();
				break;

			}
			super.handleMessage(msg);
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		AppShareApplication.getInstance().addActivity(this);
		 Bundle bundle1 = getIntent().getExtras();
	        
		 packageName=bundle1.getString("packageName");
		 appIconString=bundle1.getString("appIcon");
		 appTitle=bundle1.getString("appName");
		 pushMess=bundle1.getString("pushMessage");
		 pushTitle=bundle1.getString("pushTitle");
		 pushYOBOUrl=bundle1.getString("pushYOBOUrl");
		 pushFirstButton=bundle1.getString("pushFirstButton");
		 pushSecondButton=bundle1.getString("pushSecondButton");
		 pushStatus=bundle1.getInt("pushStatus");
		 /*
		 Log.d("MyTag","packageName="+packageName+"/appIconString="+appIconString+"/appTitle="+ 
				 appTitle+"/pushMess="+pushMess+"/pushTitle"+pushTitle+"/pushYOBOUrl"+pushYOBOUrl+"/pushFirstButton="
				 +pushFirstButton+"/pushSecondButton="+pushSecondButton+"/pushStatus="+pushStatus);
				 */
		showDialog();
	}
	
	public void showDialog()
	{
		if(!"".equals(pushTitle)&&pushTitle!=null)
		{
			if(!"".equals(appIconString)&&appIconString!=null)
			{
				
				try
				{
					appIcon= Util.getDrawableFromCache(AppshareAlarmActivity.this, appIconString);
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}else
			{
				appIcon=this.getResources().getDrawable(R.drawable.icon);
			}
			AlertDialog dialog = new AlertDialog.Builder(AppshareAlarmActivity.this).create();
			LayoutInflater inflater = LayoutInflater.from(AppshareAlarmActivity.this);
			View backupExpandHeader = inflater.inflate(R.layout.appshare_push, null);
			dialog.setCancelable(false);
			dialog.show();
			dialog.setContentView(backupExpandHeader);
			Button pushOk=(Button)backupExpandHeader.findViewById(R.id.PushOkButton);
			Button pushCancel=(Button)backupExpandHeader.findViewById(R.id.PushCancelButton);
			TextView pushTitleText=(TextView)backupExpandHeader.findViewById(R.id.PushTitleTextView);
			TextView pushMessageText=(TextView)backupExpandHeader.findViewById(R.id.PushMessageTextView);
			ImageView pushIcon=(ImageView)backupExpandHeader.findViewById(R.id.PushIconImageView);
			pushCheck=(CheckBox)backupExpandHeader.findViewById(R.id.PushCheckBox);
			
			pushTitleText.setText(pushTitle);
			pushMessageText.setText(pushMess);
			pushOk.setText(pushFirstButton);
			pushCancel.setText(pushSecondButton);
			pushIcon.setImageDrawable(appIcon);
			
			pushOk.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(pushCheck.isChecked())
					{
						AppSharePreference.setAppSharePushStatus(AppshareAlarmActivity.this,false);
					}
					if(pushStatus==1)
					{
						Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse(pushYOBOUrl));
				        it.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
				        AppshareAlarmActivity.this.startActivity(it);

					}
					if(pushStatus==2)
					{
						Intent intent = new Intent();
						intent.setClass(AppshareAlarmActivity.this, ApplicationActivity.class);
						Bundle mBundle = new Bundle();
						mBundle.putString("package_name", packageName);
						mBundle.putInt("n", 20);
						String uid = LocalStore.getLocaleStoreInstance(AppshareAlarmActivity.this).getUid();
						mBundle.putString("uid", uid);
						mBundle.putBoolean("installed_not_addicted", true);

						mBundle.putString("app_icon", "");
						PackageManager manager = AppshareAlarmActivity.this.getPackageManager();
						PackageInfo info=null;
						  try { 
						   info = manager.getPackageInfo(packageName, 0);//this.getPackageName()这个是程序包名
						  } catch (NameNotFoundException e) {
						  // TODO Auto-generated catch block
							  e.printStackTrace();
							  info=null;
						  }

	                    if(info==null)
	                    {
	                    	mBundle.putBoolean("user_app_flag", false);	
	                    }else
	                    {
	                    	mBundle.putBoolean("user_app_flag", true);
	                    }
						Drawable dw =appIcon;
						if (dw != null)
						{
							try
							{
								Bitmap bitmap = ((BitmapDrawable) dw).getBitmap();
								ByteArrayOutputStream stream = new ByteArrayOutputStream();

								bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
								byte[] bitmapdata = stream.toByteArray();
								mBundle.putByteArray("app_icon_array", bitmapdata);
							} catch (Exception ex)
							{
								ex.printStackTrace();
								mBundle.putByteArray("app_icon_array", null);
							}

						} else
						{
							mBundle.putByteArray("app_icon_array", null);
						}
						mBundle.putString("app_name", appTitle);
						intent.putExtras(mBundle);
						startActivity(intent);
						AppshareAlarmActivity.this.finish();
					}
				}
			});
			
			pushCancel.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(pushCheck.isChecked())
					{
						AppSharePreference.setAppSharePushStatus(AppshareAlarmActivity.this,false);
					}
					 AppshareAlarmActivity.this.finish();	
				}
			});
			
		/*   跳出的闹铃警示  
	    new AlertDialog.Builder(AppshareAlarmActivity.this)
	        .setIcon(appIcon)
	        .setTitle(appTitle)
	        .setMessage(pushMess).setPositiveButton("关闭",
	         new DialogInterface.OnClickListener()
	        {
	          public void onClick(DialogInterface dialog, int whichButton)
	          {
	             关闭Activity 
	        	  AppshareAlarmActivity.this.finish();
	          }
	        })
	        .setNegativeButton("进入",
	         new DialogInterface.OnClickListener()
	        {
	          public void onClick(DialogInterface dialog, int whichButton)
	          {
	        	  Intent intent = new Intent();
					intent.setClass(AppshareAlarmActivity.this, ApplicationActivity.class);
					Bundle mBundle = new Bundle();
					mBundle.putString("package_name", packageName);
					mBundle.putInt("n", 20);
					String uid = LocalStore.getLocaleStoreInstance(AppshareAlarmActivity.this).getUid();
					mBundle.putString("uid", uid);
					mBundle.putBoolean("installed_not_addicted", true);

					mBundle.putString("app_icon", "");
					PackageManager manager = AppshareAlarmActivity.this.getPackageManager();
					PackageInfo info=null;
					  try { 
					   info = manager.getPackageInfo(packageName, 0);//this.getPackageName()这个是程序包名
					  } catch (NameNotFoundException e) {
					  // TODO Auto-generated catch block
						  e.printStackTrace();
						  info=null;
					  }

                    if(info==null)
                    {
                    	mBundle.putBoolean("user_app_flag", false);	
                    }else
                    {
                    	mBundle.putBoolean("user_app_flag", true);
                    }
					Drawable dw =appIcon;
					if (dw != null)
					{
						try
						{
							Bitmap bitmap = ((BitmapDrawable) dw).getBitmap();
							ByteArrayOutputStream stream = new ByteArrayOutputStream();

							bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
							byte[] bitmapdata = stream.toByteArray();
							mBundle.putByteArray("app_icon_array", bitmapdata);
						} catch (Exception ex)
						{
							ex.printStackTrace();
							mBundle.putByteArray("app_icon_array", null);
						}

					} else
					{
						mBundle.putByteArray("app_icon_array", null);
					}
					mBundle.putString("app_name", appTitle);
					intent.putExtras(mBundle);
					startActivity(intent);
					AppshareAlarmActivity.this.finish();
	          }
	        })
	        .show();*/
		}else
		{
			AppshareAlarmActivity.this.finish();
		}
	}

}
